﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATM_Pro
{
    public partial class CommunicateBase
    {
        const string dllName = "FaceSDK.dll";
        /// Return Type: void*
        [System.Runtime.InteropServices.DllImportAttribute(dllName, EntryPoint = "FR_CreateDectection")]
        public static extern System.IntPtr FR_CreateDectection();

        ///Return Type: int
        ///handle: void*
        ///face_vector1: BYTE*
        ///len1: int
        ///face_vector2: BYTE*
        ///len2: int
        ///matchScore: double*
        [System.Runtime.InteropServices.DllImportAttribute(dllName, EntryPoint = "FR_MatchFeature")]
        public static extern int FR_MatchFeature(System.IntPtr handle, IntPtr face_vector1, int len1, IntPtr face_vector2, int len2, ref double matchScore);

        ///Return Type: int
        ///handle: void*
        ///file_path: char*
        ///face_vector: BYTE**
        ///len: int*
        [System.Runtime.InteropServices.DllImportAttribute(dllName, EntryPoint = "FR_ExtractFeature")]
        public static extern int FR_ExtractFeature(System.IntPtr handle,
            [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string file_path,
            ref System.IntPtr face_vector, ref int len);


        /// Return Type: int
        ///handle: void*
        [System.Runtime.InteropServices.DllImportAttribute(dllName, EntryPoint = "FR_ReleaseRecognization")]
        public static extern int FR_ReleaseRecognization(System.IntPtr handle);
    }

    public static class MatchFace
    {
        public static double StartMacth(string face1, string face2, ref bool res)
        {
            IntPtr face_v1 = IntPtr.Zero;
            IntPtr face_v2 = IntPtr.Zero;
            int len1 = 0;
            int len2 = 0;
            double matchVal = 0.0;
            IntPtr faceRec = CommunicateBase.FR_CreateDectection();
            try
            {
                CommunicateBase.FR_ExtractFeature(faceRec, face1, ref face_v1, ref len1);
                CommunicateBase.FR_ExtractFeature(faceRec, face2, ref face_v2, ref len2);
                CommunicateBase.FR_MatchFeature(faceRec, face_v1, len1, face_v2, len2, ref matchVal);
                CommunicateBase.FR_ReleaseRecognization(faceRec);
                res = true;
            }
            catch (Exception ex)
            {
                CommunicateBase.FR_ReleaseRecognization(faceRec);
                Console.WriteLine(ex.Message);
                res = false;
            }
            return matchVal;
        }
    }
}
